package 蓝桥杯算法;

import 链表.Node1;

import javax.swing.undo.CannotUndoException;
import java.util.Arrays;

public class day03 {
    public static void main(String[] args) {
        LinkedList list=new LinkedList();
        list.addNode(1);
        list.addNode(2);
        list.addNode(3);
        list.addNode(4);

    }
    public int removeElement(int[] num,int val){//暴力删除
        int size=num.length;
        for(int i=0;i<size;i++){
            if(num[i]==val){
                for(int j=i;j<size;j++){
                    num[j]=num[j+1];
                }
                i--;//防止下一个数字也与val相等
                size--;
            }
        }
        return size;
    }
    public int removeElement2(int[] num,int val){//指针法
        int size=num.length;
        int slow=0;
        for(int fast=0;fast<size;fast++){
            if(num[fast]!=val){//每次都赋值，在索引不同的时候复制显示出来
                num[slow]=num[fast];
                slow++;
            }
        }
        return slow;
    }
    public  int[] sortedSquares1(int[] num){//暴力排序
        for(int i=0;i<num.length;i++){
            num[i]=num[i]*num[i];
        }
        Arrays.sort(num);//排序
        return num;
    }
    public int[] sortedSquares2(int[] num){
        int right=num.length-1;
        int left=0;
        int[] result=new int[num.length];
        int index=right-1;
        while(left<right){
            if(num[left]*num[left]>num[right]*num[right]){//左侧的大
                result[index--]=num[left++];
            }
            else if(num[left]*num[left]<num[right]*num[right]){
                result[index--]=num[right--];
            }
            else{
                result[index--]=num[left++];
                right--;
            }
        }
        return result;
    }
    public  int minSubArray(int[] nums,int val){//暴力
       int result=nums.length;
       int sum=0;
       int subLength=nums.length;
       for(int i=0;i<nums.length;i++){
           for(int j=i;j<nums.length;j++){
               sum+=nums[j];
               if(sum>=val){
                   subLength=j-i+1;
                   result=result<subLength?result:subLength;
                   break;
               }
           }
       }
       return result;
    }
    public  int minSubArray2(int[] nums,int val){//滑动窗户
        int result=nums.length;
        int sum=0;
        int left=0;
        for(int right=0;right<nums.length;right++){
            sum+=nums[right];
            while(sum>=val){//窗口的移动
                result=Math.min(result,right-left+1);
                sum-=nums[left++];
            }
        }
        return result==nums.length?0:result;
    }
    static class Node{//创建链表
        int value;
        Node next;
        public Node(int value){
            this.next=next;
            this.value=value;
        }
    }
    static class LinkedList{ //创建链表
        int size;
        Node head;
        public LinkedList(){
            this.size=0;
            this.head=new Node(0);
        }
        public void addNode(int value){
            Node newNode=new Node(value);
            if(head==null){
                head=newNode;
            }
            else {
                Node current=head;
                while(current.next!=null){
                    current=current.next;
                }
                current.next=newNode;
            }
        }
        public int getElement(int index){
            if(index<0||index>=size){
              return -1;
            }
            Node current=head;
            for(int i=0;i<index;i++){
                current=current.next;
            }
            return current.value;
    }
    public  void addAtHead(int value){
            Node newNode=new Node(value);
            if(head==null){
                head=newNode;
            }
            else{
                Node current=head.next;
                head.next=newNode;
                newNode.next=current;
            }
    }
    public void addAtTail(int value){
            Node newNode=new Node(value);
            if(head==null){
                head=newNode;
            }
            else {
                Node current=head;
                while(current.next==null){
                    current.next=newNode;
                }
            }
    }
    public int deleteNode(int index){
            if(index<0||index>size){
                return -1;
            }
            else {
                Node current=head;
                for(int i=0;i<index;i++){//获取它的前一个
                    current=current.next;
                }
                current.next=current.next.next;
                size--;
            }
            return index;
    }




    }

}
